package com.tueye.dao.impl.userblog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tueye.base.util.BaseDao;
import com.tueye.base.util.IBaseHbnDao;
import com.tueye.base.util.Page;
import com.tueye.dao.userblog.IUserBlogDao;
import com.tueye.entity.user.GoodFriend;
import com.tueye.entity.user.User;
import com.tueye.entity.user.UserBlogInfo;
import com.tueye.entity.user.UserCollectionInfo;

/**
 * 
 * @author hanxiaowei
 *
 */
@Scope("prototype")
@Component("userBlogDao")
public class UserBlogDaoImpl extends BaseDao implements IUserBlogDao{
	
	@Autowired
	@Qualifier("baseHbnDao")
	private IBaseHbnDao  baseHbnDao;
	
	@Override
	public UserBlogInfo getUserBlogInfo(String id) {
		return (UserBlogInfo) baseHbnDao.findById(UserBlogInfo.class, id);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page pagedQueryByHQL(String userid, int pageNo, int pageSize,
			Object... values) {
		//queryList
		List<UserCollectionInfo> ls = new ArrayList<UserCollectionInfo>();
		ls = baseHbnDao.queryList("from com.tueye.entity.user.UserCollectionInfo u  where u.userTopicType=1 and u.user ='"+userid+"' ");
		if(!ls.isEmpty()){
			StringBuffer sb = new StringBuffer(1024);
			for (int i = 0; i < ls.size(); i++) {
				UserCollectionInfo uci = (UserCollectionInfo) ls.get(i);
				sb.append(",'"+uci.getTopics().getId()+"'");
			}
			sb.delete(0, 1);
			String hql = "from com.tueye.entity.Pictures p where p.topics in("+sb.toString()+") and main_picture =1 ";
			return baseHbnDao.pagedQueryByHQL(hql, pageNo, pageSize, values);
		}
	
		return null;
	}

	@Override
	public com.tueye.entity.user.User getUserById(String userid){
	
		return (com.tueye.entity.user.User) baseHbnDao.findById(com.tueye.entity.user.User.class, userid) ;
	}

	@Override
	public Map getFlowerNum(String userid) {
		Map FlowerNumArry = new HashMap();
		List sumNum = new ArrayList();
		List<UserCollectionInfo> ls = new ArrayList<UserCollectionInfo>();
		ls = baseHbnDao.queryList("from com.tueye.entity.user.UserCollectionInfo u  where u.userTopicType=1 and u.user ='"+userid+"' ");
		if(!ls.isEmpty()){
			StringBuffer sb = new StringBuffer(1024);
			for (int i = 0; i < ls.size(); i++) {
				UserCollectionInfo uci = (UserCollectionInfo) ls.get(i);
				sb.append(",'"+uci.getTopics().getId()+"'");
			}
			sb.delete(0, 1);
			//鲜花个数
			String flowersNumsql = "select sum(flowersNum) from com.tueye.entity.user.Flowers f where f.topics in("+sb.toString()+")";
			sumNum = baseHbnDao.queryList(flowersNumsql);
			String flowersNum =getObjectString(sumNum,"flowersNum");
			FlowerNumArry.put("flowersNum", flowersNum);
			
			//有才个数
			String CaiNumsql = "select sum(caiNum) from com.tueye.entity.user.Flowers f where f.topics in("+sb.toString()+")";
			sumNum = baseHbnDao.queryList(CaiNumsql);
			String CaiNum =getObjectString(sumNum,"caiNum");
			FlowerNumArry.put("caiNum", CaiNum);
			
			//拍的好个数
			String PaiNumsql = "select sum(paiNum) from com.tueye.entity.user.Flowers f where f.topics in("+sb.toString()+")";
			sumNum = baseHbnDao.queryList(PaiNumsql);
			String PaiNum =getObjectString(sumNum,"paiNum");
			FlowerNumArry.put("paiNum", PaiNum);
			
			//流口水个数
			String KouNumsql = "select sum(kouNum) from com.tueye.entity.user.Flowers f where f.topics in("+sb.toString()+")";
			sumNum = baseHbnDao.queryList(KouNumsql);
			String KouNum =getObjectString(sumNum,"kouNum");
			FlowerNumArry.put("kouNum", KouNum);
			
			//赞一个数
			String ZanNumsql = "select sum(zanNum) from com.tueye.entity.user.Flowers f where f.topics in("+sb.toString()+")";
			sumNum = baseHbnDao.queryList(KouNumsql);
			String ZanNum =getObjectString(sumNum,"zanNum");
			FlowerNumArry.put("zanNum", ZanNum);
			
		}
		return FlowerNumArry;
	}
	private String getObjectString(List ls,String objectname ){
		
		if (ls.isEmpty()) return "0";
		if((Object) ls.get(0) == null){
			
			return "0";
			
		}else{
			return ((Object) ls.get(0)).toString();
		}
	}

	@Override
	public List getGoodFriendToUserInfo(String userid) {
		List<User> userList = new ArrayList<User>();
		List<GoodFriend> ls = new ArrayList<GoodFriend>();
		ls = baseHbnDao.queryList("from com.tueye.entity.user.GoodFriend u  where u.accept_req=1 and user_id ='"+userid+"' ");
		if(!ls.isEmpty()){
			StringBuffer sb = new StringBuffer(1024);
			for (int i = 0; i < ls.size(); i++) {
				GoodFriend gf = (GoodFriend) ls.get(i);
				sb.append(",'"+gf.getFriend_id()+"'");
			}
			sb.delete(0, 1);
			String hql = "from com.tueye.entity.user.User where id in("+sb.toString()+")";
			userList = (List)baseHbnDao.pagedQueryByHQL(hql, 0, 4, null).getResult();
		}
		return userList;
	}
	public String isGoodFriend(String userid) {
		String retuln="";
		List<GoodFriend> ls = new ArrayList<GoodFriend>();

		ls= baseHbnDao.queryList("from com.tueye.entity.user.GoodFriend u  where u.accept_req=1 and user_id ='"+userid+"'");
		if(ls.size()>0){
			if(ls.size()>0){retuln= "1";}
		}else{
			ls = baseHbnDao.queryList("from com.tueye.entity.user.GoodFriend u  where u.accept_req=2 and user_id ='"+userid+"'");
			if(ls.size()>0){retuln= "2";}else{
				ls = baseHbnDao.queryList("from com.tueye.entity.user.GoodFriend u  where u.accept_req=3 and user_id ='"+userid+"'");
				if(ls.size()>0){retuln= "3";}
			}
		}
		
		
 		return retuln;
	}
	@Override
	public void saveGoodFriend(GoodFriend goodFriend) {
		baseHbnDao.saveOrUpdate(goodFriend);
		
	}

}
